//
//  CustomTableViewCell.m
//  NBTripe
//
//  Created by Lee on 16/1/11.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "CustomTableViewCell.h"

@interface CustomTableViewCell ()

@property (nonatomic, copy) NSString * str;

@end

@implementation CustomTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.manager = [CoreDataManager shareManager];
        //self.storage = [[Storage alloc] init];
        [self createView];
    }
    return self;
}

- (void)createView {
    self.imageview = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.backview = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.backview.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    
    self.title = [[UILabel alloc] initWithFrame:CGRectZero];
    self.title.textColor = [UIColor whiteColor];
    self.title.numberOfLines = 0;
    self.title.font = [UIFont boldSystemFontOfSize:18];
    self.title.numberOfLines = 0;
    _title.textAlignment = NSTextAlignmentLeft;
    
    self.marklabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.marklabel.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
    
    self.startdaylabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.startdaylabel.textColor = [UIColor whiteColor];
    self.startdaylabel.font = [UIFont boldSystemFontOfSize:13];
   // self.startdaylabel.backgroundColor = [UIColor purpleColor];
    
    self.daylabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.daylabel.textColor = [UIColor whiteColor];
    self.daylabel.font = [UIFont boldSystemFontOfSize:13];
   // self.daylabel.backgroundColor = [UIColor purpleColor];
    
    self.scancountlabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.scancountlabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.scancountlabel.textColor = [UIColor whiteColor];
    self.scancountlabel.font = [UIFont boldSystemFontOfSize:13];
    
    self.placelabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.placelabel.textColor = [UIColor whiteColor];
    self.placelabel.font = [UIFont boldSystemFontOfSize:13];
    
    self.headimageview = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.authorlabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.authorlabel.textColor = [UIColor whiteColor];
    self.authorlabel.font = [UIFont systemFontOfSize:14];
    //收藏
    self.storageButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.storageButton setImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
 
    [self.storageButton addTarget:self action:@selector(shoucang:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.imageview];
    [self.contentView addSubview:self.storageButton];
    [self.imageview addSubview:self.backview];
    [self.backview addSubview:self.title];
    [self.backview addSubview:self.marklabel];
    [self.backview addSubview:self.startdaylabel];
    [self.backview addSubview:self.daylabel];
    [self.backview addSubview:self.scancountlabel];
    [self.backview addSubview:self.placelabel];
    [self.backview addSubview:self.headimageview];
    [self.backview addSubview:self.authorlabel];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _imageview.frame = CGRectMake(10, 10, self.frame.size.width-20, self.frame.size.height-10);
    _imageview.layer.masksToBounds = YES;
    _imageview.layer.cornerRadius = 5*XWIDTH;
    _backview.frame = CGRectMake(-10, 0, self.frame.size.width, self.frame.size.height-10);
    
    self.title.frame = CGRectMake(25*XWIDTH, 0, WIDTH-80, 60*XHIGHT);
    self.storageButton.frame = CGRectMake(WIDTH-50*XWIDTH, 10*XHIGHT, 25*XWIDTH, 25*XHIGHT);
    self.marklabel.frame = CGRectMake(25*XWIDTH, _title.bottom+5, 3*XWIDTH, 23*XHIGHT);
    self.marklabel.layer.masksToBounds = YES;
    self.marklabel.layer.cornerRadius = 2*XWIDTH;
    self.startdaylabel.frame = CGRectMake(self.marklabel.right+5, self.marklabel.origin.y, 100*XWIDTH, 10*XHIGHT);
    
    self.daylabel.frame = CGRectMake(self.startdaylabel.right, self.marklabel.origin.y, 40*XWIDTH, 10*XHIGHT);
    
    self.scancountlabel.frame = CGRectMake(self.daylabel.right, self.marklabel.origin.y, 100*XWIDTH, 10*XHIGHT);
    
    self.placelabel.frame = CGRectMake(self.marklabel.right+5, self.marklabel.origin.y+13, 200*XWIDTH, 10*XHIGHT);
    
    self.headimageview.frame = CGRectMake(self.title.origin.x, self.height-60, 30*XWIDTH, 30*XWIDTH);
    self.headimageview.layer.masksToBounds = YES;
    self.headimageview.layer.cornerRadius = self.headimageview.width/2;
    self.headimageview.backgroundColor = [UIColor redColor];
    self.authorlabel.frame = CGRectMake(self.headimageview.right+10, self.headimageview.origin.y+10, 200*XWIDTH, 20*XHIGHT);
   
}
- (void)setModel:(RecommendSecondModel *)model {
    if (_model != model) {
        _model = model;
    }
    [_imageview sd_setImageWithURL:[NSURL URLWithString:_model.cover_image_w640] placeholderImage:[UIImage imageNamed:@"160.jpg"]];
    self.title.text = _model.name;
    self.daylabel.text = [NSString stringWithFormat:@"%@ 天", _model.day_count.stringValue];
    self.startdaylabel.text = _model.first_day;
    self.scancountlabel.text = [NSString stringWithFormat:@"%@ 浏览", _model.view_count.stringValue];
    self.placelabel.text = _model.popular_place_str;
 
    
    //判断是否收藏过
    _str = [Convenience selectIdentifierWithManager:self.manager managerWithStr:_model.joinedId.stringValue];
    //NSLog(@"-----%@", _str);
    if (_str != nil) {
        self.storageButton.tintColor = [UIColor redColor];
    }
    else {
        self.storageButton.tintColor = [UIColor whiteColor];
    }
    [self.storageButton addTarget:self action:@selector(shoucang:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)setUsermodel:(UserModel *)usermodel {
    if(_usermodel != usermodel){
        _usermodel = usermodel;
    }
    self.authorlabel.text = [NSString stringWithFormat:@"by %@", _usermodel.name];
    [self.headimageview sd_setImageWithURL:[NSURL URLWithString:_usermodel.avatar_m] placeholderImage:[UIImage imageNamed:@"160.jpg"]];
    
}
- (void)shoucang:(UIButton *)sender {
   

    if (sender.tintColor == [UIColor whiteColor]) {
        //储藏赋值  插入实体，参数1:实体名  参数2:数据管理器
        self.storage = [NSEntityDescription insertNewObjectForEntityForName:@"Storage" inManagedObjectContext:self.manager.managedObjectContext];
        self.storage.title = _model.name;
        self.storage.photo = _model.cover_image_w640;
        self.storage.joinid = _model.joinedId.stringValue;
        NSLog(@"+++++++%@", self.storage);
        [self.manager saveContext];
        sender.tintColor = [UIColor redColor];
    }
    else if(sender.tintColor == [UIColor redColor]){
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        NSArray *arr = [Convenience selectLocalCorearr:self.manager managerWithstr:_model.joinedId.stringValue];
        //NSLog(@"%@", self.storage);
        [center postNotificationName:@"alertView" object:nil userInfo:@{@"storage":arr, @"button":sender}];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
